<?php
namespace app\model;
use app\utils\CamlHump;
use think\facade\Db;
use think\Model;
use think\model\concern\SoftDelete;

class BaseModel extends Model
{
    use SoftDelete;
    protected $deleteTime = 'del_time';
    protected $dateFormat = 'Y-m-d H:i:s';
    protected $autoWriteTimestamp = 'datetime';





    static function  build($data)
    {
        $emptyData = [];

        foreach (static::$tableKeys as $key => $value){
            $emptyData[CamlHump::toCamelCase($key)] = $data[CamlHump::toCamelCase($key)] ?? '';
        }
        return $emptyData;
    }
    static function  buildForUpdate($data)
    {
        $updateData = [];

        foreach (static::$tableTable as $key => $value){
            if (isset($data[CamlHump::toCamelCase($key)])) {
                $updateData[CamlHump::toCamelCase($key)] = $data[CamlHump::toCamelCase($key)];
            }
        }

        return $updateData;
    }
    static function createQuery($queryData)
    {
        $query = Db::table(static::$tableTable);
        $query->where('del_state', 1);


        foreach (static::$tableKeys as $key => $value){
            if (isset($queryData[CamlHump::toCamelCase($key)])
            ) {
                if($value=='int'){
                    if($queryData[CamlHump::toCamelCase($key)]>0){
                        $query->where($key, $queryData[CamlHump::toCamelCase($key)]);
                    }
                }else if($value=='string'){
                    if($queryData[CamlHump::toCamelCase($key)]!=''){
                        $query->where($key, $queryData[CamlHump::toCamelCase($key)]);
                    }
                }else{
                    $query->where($key, $queryData[CamlHump::toCamelCase($key)]);
                }
            }

        }


        return $query;
    }



    static function addPlus($data){
        $sysAccountAdd = static::build($data);
        $result = static::create($sysAccountAdd);
        return $result;
    }
    static function updatePlus($data, $id)
    {
        $result = static::update($data, ['id' => $id]);
        return $result;
    }

    static function deletePlus($id)
    {
        $result = static::destroy($id);
        return $result;
    }

    static function findOne($id){
        $query=Db::table(static::$tableTable)->where("id", $id)->where('del_state',1);
        $one = CamlHump::toCamlObj($query->find());
        return $one;
    }


    public static function onBeforeInsert($data)
    {

        $data['tenantType'] = '';
        $data['tenantId'] = '';
        $data['delState'] = 1;
        $data['addUserId'] = '';
        $data['addTime'] = date("Y-m-d H:i:s", time());
        $data['addUser'] = '';
        $data['editUser'] = '';
        $data['editUserId'] = '';
        $data['lastEditTime'] = date("Y-m-d H:i:s", time());
        $data['serverNumber'] = '';
        $data['del_time'] = NULL;

        return $data;
    }
    public static function onBeforeUpdate($data)
    {
//        $data['tenantType'] = '';
//        $data['tenant_id'] = '';
//        $data['del_state'] = 1;
//        $data['add_user'] = '';
//        $data['add_user_id'] = '';
//        $data['add_time'] = time();
//        $data['add_user'] = '';
        $data['editUser'] = '';
        $data['editUserId'] = '';
        $data['lastEditTime'] = date("Y-m-d H:i:s", time());
        $data['del_time'] = NULL;
//        $data['server_number'] = '';
        return $data;
    }
    public static function onBeforeDelete($data)
    {
        $data['delState'] = 5;
        $data['delTime'] = date("Y-m-d H:i:s", time());
        return $data;
    }
}